package com.rshang.rsdp.service.impl;

import com.rshang.rsdp.entity.TbLog;
import com.rshang.rsdp.entity.TbUser;
import com.rshang.rsdp.kit.HttpKits;
import com.rshang.rsdp.kit.StrKits;
import com.rshang.rsdp.mapper.TbLogMapper;
import com.rshang.rsdp.service.ITbLogService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author zhangjun
 * @since 2017-08-10
 */
@Service
public class TbLogServiceImpl extends ServiceImpl<TbLogMapper, TbLog> implements ITbLogService {

	private static final String LOG_CONTENT = "[类名]:%s,[方法]:%s,[参数]:%s,[IP]:%s";
	
	@Override
	public void save(ProceedingJoinPoint joinPoint, String methodName, String module, String description) {
		
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

		TbUser user = null;
        Subject currentUser = SecurityUtils.getSubject();
		if(currentUser.isAuthenticated()&&currentUser.getPrincipal()!=null){
			user =(TbUser) currentUser.getPrincipal();
		}
		
        String ip = HttpKits.getIpAddr(request);

        TbLog log = new TbLog();
        log.setLogId(StrKits.uuid());
        log.setLogUrl(request.getRequestURI());
        log.setLogUser(user!=null?user.getUserName():StrKits.emptyToStr(request.getParameter("username")));
        log.setLogAddr(ip);
        log.setLogName(module);
        log.setLogDesc(description);
        log.setLogCont(operateContent(joinPoint, methodName, ip, request));

        baseMapper.insert(log);
	}
	
	
	/**
	 * 操作字符串内容组成方法
	 * @param joinPoint
	 * @param methodName
	 * @param ip
	 * @param request
	 * @return
	 * 		String
	 */
	public String operateContent(ProceedingJoinPoint joinPoint, String methodName, String ip, HttpServletRequest request) {
        String className = joinPoint.getTarget().getClass().getName();
        Object[] params = joinPoint.getArgs();
        StringBuffer bf = new StringBuffer();
        if (params != null && params.length > 0) {
            Enumeration<String> paraNames = request.getParameterNames();
            while (paraNames.hasMoreElements()) {
                String key = paraNames.nextElement();
                bf.append(key).append("=");
                bf.append(request.getParameter(key)).append("&");
            }
            if (StringUtils.isBlank(bf.toString())) {
                bf.append(request.getQueryString());
            }
        }
        return String.format(LOG_CONTENT, className, methodName, bf.toString(), ip);
    }
}
